<template>
    <div class="login-container">
      <h2>管理员登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="input-group">
          <label for="admin-username">ID:</label>
          <input type="text" id="admin-username" v-model="ID" required />
        </div>
        <div class="input-group">
          <label for="admin-password">密码:</label>
          <input type="password" id="admin-password" v-model="password" required />
        </div>
        <button type="submit">登录</button>
      </form>
    </div>
  </template>
  
  <script setup>
  import { ref } from 'vue';
  import { useRouter } from 'vue-router';
  import axios from 'axios';
  
  const ID = ref('');
  const password = ref('');
  const router = useRouter();
  
  const handleLogin = () => {
    console.log('Admin Logging in with:', { ID: ID.value, password: password.value });
    if(ID.value !='' && password.value !='') {
      const formData = new URLSearchParams();
      formData.append('ID', ID.value);
      formData.append('password', password.value);
      const response = axios.post('http://localhost:8080/adminlogin', formData);
      response.then(res => {
      console.log(res);
      if (res.data.success) {
        // 登录成功，跳转到管理员Dashboard
        router.push('/admin-dashboard');
      } else {
        alert('登录失败, 请检查ID和密码');
      }
      });
  } else {
      alert('ID或密码不能为空');
    }
  }
  </script>
  
  <style scoped>
  .login-container {
    max-width: 400px;
    margin: 100px auto;
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    text-align: center;
  }
  
  h1 {
    color: #333;
    margin-bottom: 20px;
  }
  
  .input-group {
    margin-bottom: 15px;
  }
  
  input[type="text"],
  input[type="password"] {
    width: 100%;
    padding: 10px;
    margin-top: 5px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box; /* 使 padding 不影响宽度 */
  }
  
  button {
    width: 100%;
    padding: 10px;
    border: none;
    border-radius: 4px;
    background-color: #5cb85c; /* 绿色按钮 */
    color: white;
    font-size: 16px;
    cursor: pointer;
    margin-top: 10px;
  }
  
  button:hover {
    background-color: #4cae4c; /* 鼠标悬停时的按钮颜色 */
  }
  
  .register-link {
    display: inline-block;
    margin-top: 20px;
    color: #5cb85c;
    text-decoration: none;
    font-size: 14px;
  }
  
  .register-link:hover {
    text-decoration: underline;
  }
</style>